
	********************************************************************************
	****
	**** Date: 04.25.2022							 
	**** Update: 07.11.2023					 
	**** Author: JW josephgwright@gmail.com 				 	
	**** NOTE: This program has been executed in Stata 18.0	 
	**** 
	**** Using .dta in $dir: 
	****
	****  	GWFglobal2020.dta
	****    erdogan-populism.dta
	**** 	temp.dta
	****	pers-use.dta
	****
	*********************************************************************************
 
 
		
		capture log close
		log using Ch1.log, replace

	******************************
	**** Set directory, seed *****
	******************************
		set more off 
		set matsize 1000
		global seed ="984353"
		set scheme plotplain
		cd "$dir"
 
	************************************************
	**** Global patterns of democratic collapse ****
	************************************************
		use  "GWFglobal2020.dta" ,clear
		 * Update for collapse in 2020 *
		recode gwf_back (0=1) if year==2020 & (gwf_country=="Mali")
		tab gwf_back
		egen cy = count(gwf_back) if gwf_back==1,by(year)
		egen collapserate = mean(gwf_back) if gwf_democracy==1 | gwf_provisional==1,by(year)
		sort cowcode year
		merge cowcode year using pers-use
		tab _merge
		drop _merge
		qui reg persparty ld
		predict yhatpers, r
		qui sum yhatpers
		replace yhatpers = (yhatpers + abs(r(min)))/(abs(r(min))+r(max))
		egen yrpers = mean(yhatpers),by(year)
		qui reg create ld 
		predict yhatcreate, r
		qui sum yhatcreate
		replace yhatcreate = (yhatcreate + abs(r(min)))/(abs(r(min))+r(max))
		egen yrcreate = mean(yhatcreate),by(year)

  		gen backtype=""
		replace backtype ="rebellion" if country=="Afghanistan" & year==2019
		replace backtype ="powergrab" if country=="Armenia" & year==1994
		replace backtype ="rebellion" if country=="Azerbaijan" & year==1993
		replace backtype ="powergrab" if country=="Bangladesh" & year==2014
		replace backtype ="powergrab" if country=="Benin" & year==2019
		replace backtype ="coup" if country=="Bolivia" & year==2019
		replace backtype ="coup" if country=="Burundi" & year==1996
		replace backtype ="powergrab" if country=="Burundi" & year==2010
		replace backtype ="rebellion" if country=="Central African Republic" & year==2003
		replace backtype ="coup" if country=="Egypt" & year==2013
		replace backtype ="powergrab" if country=="Guinea Bissau" & year==2002
		replace backtype ="powergrab" if country=="Haiti" & year==1999
		replace backtype ="powergrab" if country=="Hungary" & year==2018
		replace backtype ="coup" if country=="Madagascar" & year==2009
		replace backtype ="coup" if country=="Mali" & year==2012
		replace backtype ="coup" if country=="Mali" & year==2020
		replace backtype ="coup" if country=="Mauritania" & year==2008
		replace backtype ="powergrab" if country=="Nicaragua" & year==2016
		replace backtype ="coup" if country=="Niger" & year==1996
		replace backtype ="powergrab" if country=="Niger" & year==2009
		replace backtype ="coup" if country=="Pakistan" & year==1999
		replace backtype ="powergrab" if country=="Peru" & year==1992
		replace backtype ="rebellion" if country=="Republic of Congo" & year==1997
		replace backtype ="powergrab" if country=="Russia" & year==1993
		replace backtype ="powergrab" if country=="Serbia" & year==2018
		replace backtype ="rebellion" if country=="Sierra Leone" & year==1997
		replace backtype ="powergrab" if country=="Sri Lanka" & year==2010
		replace backtype ="powergrab" if country=="Sri Lanka" & year==2020
		replace backtype ="coup" if country=="Thailand" & year==1991
		replace backtype ="coup" if country=="Thailand" & year==2006
		replace backtype ="coup" if country=="Thailand" & year==2014
		replace backtype ="powergrab" if country=="Turkey" & year==2016
		replace backtype ="powergrab" if country=="Ukraine" & year==2012
		replace backtype ="powergrab" if country=="Venezuela" & year==2005
		replace backtype ="powergrab" if country=="Zambia" & year==1996
		
		gen grabback = backtype=="powergrab" if gwf_democracy ==1  & year>1990
		gen coupback = backtype=="coup" if gwf_democracy ==1  & year>1990
		
		egen cy_coup = total(coupback) if persparty~=.,by(year)
		egen cy_grab = total(grabback) if persparty~=.,by(year)
		twoway (lpoly cy_grab year if year>1990,bw(3)xlab(1990(5)2020)ylab(0(.5)1)) ///
			(lpoly cy_coup year if year>1990,bw(3)legend(lab(1 "Democratic collapse by {bf:incumbent power grab}") ///
			lab(2 "Democratic collapse by {bf:coup}")pos(12)ring(0)col(1))xtit(Year) ytit("Yearly average  #"))
		gr export "$dir\golden\Ch1-Dem-Collapse.pdf",as(pdf)replace 

	*********************************************  THE END *************************************************
	
	log close